#ifndef _LUABIND_READLINE_H
#define _LUABIND_READLINE_H

#include "lua/z9_lua.h"

#include "linenoise.h"

void completion(const char *buf, linenoiseCompletions *lc) {

	switch(buf[0]){
	case 'r':{
		linenoiseAddCompletion(lc,"redis_execute");
		linenoiseAddCompletion(lc,"result");
			 }
			 break;
	case 'e':{
		linenoiseAddCompletion(lc,"exit");  
			 }
			 break;
	case 'p':{
		linenoiseAddCompletion(lc,"print");   		
			 }
			 break;
	default:{
		return;
			}

	}
}

int32_t z9_lua_readline(lua_State *L)
{
	const char *prompt = lua_tostring(L,1);
	if(!prompt) {
		return 0;
	}

	char *line = linenoise(prompt);

	if(NULL != line) {

		if(*line) {
			linenoiseHistoryAdd(line);
		}
		lua_pushstring(L,line);
		free(line);
		return 1;
	}
	else {
		return 0;
	}
}



//int32_t luaopen_readline(lua_State *L)
//{
//	//lua_newtable(L);
//	//SET_FUNCTION(L,"Readline",z9_lua_readline);
//	//linenoiseHistoryLoad("history.txt");
//	linenoiseSetCompletionCallback(completion);	
//	//lua_pushcfunction(L,z9_lua_readline); //zzh del this 
//	lua_register(L, "Readline", z9_lua_readline); //zzh add 20170110
//	return 1;
//}

//zzh add 
void register_readline(lua_State *L) {
	linenoiseSetCompletionCallback(completion);	
	lua_register(L, "Readline", z9_lua_readline);
}

#endif 
